package com.lhd.qd.module.system.user.controller;

import com.lhd.qd.common.base.ApiResult;
import com.lhd.qd.common.base.BaseController;
import com.lhd.qd.common.consts.RedisConsts;
import com.lhd.qd.common.util.RedisUtils;
import com.lhd.qd.common.util.TokenUtils;
import com.lhd.qd.module.system.user.model.dto.LoginDTO;
import com.lhd.qd.module.system.user.model.vo.LoginVO;
import com.lhd.qd.module.system.user.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Api(tags = "登录")
@RestController
public class LoginController extends BaseController {

    @Autowired
    private LoginService loginService;

    @ApiOperation(value = "登录", response = LoginVO.class)
    @PostMapping("login")
    public ResponseEntity<ApiResult<LoginVO>> login(@Validated @RequestBody LoginDTO dto, @RequestHeader(required = false) Map<String, String> clientMap) {

        return success(loginService.login(dto, clientMap));
    }

    @ApiOperation(value = "登出")
    @PostMapping("logout")
    public ResponseEntity logout(@RequestBody String token) {

        RedisUtils.delete(RedisConsts.KEY_USER_INFO + TokenUtils.parse(token));

        return success();
    }
}
